//
//  NRWMyCollectionVC.m
//  NRWMall
//
//  Created by 张记 on 2018/6/4.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "NRWMyCollectionVC.h"
#import "NRWCarListCell.h"
#import "NRWCarDetailVC.h"
#import "NRWCollectCarListApi.h"
#import "NRWBuyCarListDataModel.h"
#import "NRWBuyCarListRectModel.h"

@interface NRWMyCollectionVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong) UITableView *tableview;
@property(nonatomic, strong) NRWCollectCarListApi *collectCarListApi;
//@property(nonatomic, strong) NRWBuyCarListDataModel *dataModel;
@property (strong, nonatomic) NSArray<NRWBuyCarListRectModel *> *dataModel;
//@property(nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation NRWMyCollectionVC
-(NRWCollectCarListApi *)collectCarListApi{
    if (!_collectCarListApi) {
        _collectCarListApi  = [[NRWCollectCarListApi alloc] init];
        _collectCarListApi.delegate = self;
    }
    return _collectCarListApi;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationView setTitle:@"我的收藏"];
    [self initTableview];
    [SVProgressHUD show];
    self.collectCarListApi.pageIndex = 1;
    [self.collectCarListApi start];
}
#pragma mark - setupUI
-(void)initTableview{
    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.backgroundColor = backColor();
    [self.tableview registerClass:[NRWCarListCell class] forCellReuseIdentifier:@"NRWCarListCell"];
    self.tableview.estimatedRowHeight = 0;
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    self.tableview.tableFooterView = DPView();
	[self.tableview addFooterRefreshWithTarget:self action:@selector(loadMoreData)];
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).mas_offset(getNavHeight());
        make.left.right.bottom.equalTo(weakSelf.view);
    }];
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

#pragma mark - DelegateMethod
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataModel.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
    NRWCarListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NRWCarListCell" forIndexPath:indexPath];
	cell.carFrameModel = self.dataModel[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 100;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    Result *rt = self.dataModel[indexPath.row].dataModel;
    NRWCarDetailVC *DetailVc = [[NRWCarDetailVC alloc] init];
    DetailVc.carId = rt.carId;
    DetailVc.thisCarModel = rt;
    [self.navigationController pushViewController:DetailVc animated:YES];
    
}
-(void)loadMoreData{
    [SVProgressHUD show];
    self.collectCarListApi.pageIndex++;
    [self.collectCarListApi start];
}
-(void)reloadData{
    self.collectCarListApi.pageIndex++;
    [self.collectCarListApi start];
}
- (void)requestFinished:(__kindof YTKBaseRequest *)request {
    if (request == self.collectCarListApi) {
		
        NRWBuyCarListDataModel *model = [NRWBuyCarListDataModel mj_objectWithKeyValues:request.responseObject];
        if (model.code == 1) {
			if (self.collectCarListApi.pageIndex == 1) {
				self.dataModel = nil;
			}
			NSMutableArray *arrM = [NSMutableArray arrayWithArray:self.dataModel];
            for (Result *carModel in model.result) {
				NRWBuyCarListRectModel *rectModel = [NRWBuyCarListRectModel new];
				rectModel.dataModel = carModel;
                [arrM addObject:rectModel];
            }
			self.dataModel = arrM.copy;
            [self.tableview reloadData];
            
        }else{
            
            [NRWMessageHUD showMessage:request.responseObject[@"msg"] forTime:2.0 withRemindId:remindFail];
        }
        if (self.dataModel.count == 0) {
            kSelfWeak;
            [self.tableview showMaskViewWithTitle:@"暂无收藏" imageName:@"default_empty" titleToImageMargin:42 canScroll:NO didClick:^{
                weakSelf.collectCarListApi.pageIndex = 1;
                [weakSelf.collectCarListApi start];
            }];
        }else{
            [self.tableview removeMaskView];
        }
        [self.tableview.mj_footer endRefreshing];
    }
    [SVProgressHUD dismiss];
    
}

- (void)requestFailed:(__kindof YTKBaseRequest *)request {
    [self.tableview.mj_footer endRefreshing];
    [SVProgressHUD dismiss];
    [NRWMessageHUD showMessage:@"请求失败" forTime:2.0 withRemindId:remindFail];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
